package com.ray.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.ray.exception.ServiceException;
import com.ray.service.FileManagerService;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.baidu.BaiduProductPicSearchUtils;
import com.ray.util.baidu.BaiduSamePicSearchUtils;
import com.ray.util.baidu.BaiduSimilarPicSearchUtils;
import com.ray.util.baidu.Brief;
import com.ray.util.baidu.ImageUtils;
import com.ray.util.baidu.Tags;
import com.ray.util.qiniu.QiniuExtraUrlParamUtils;
import com.ray.util.validate.GetParamUtils;

/**
 * baidu图片服务
 * @author Shensg
 *
 * 2018年10月9日
 */

@SuppressWarnings("unused")
@Controller
@RequestMapping("/baiduPic")
public class BaiduPicController {
	
	//图片入库
	/**
	 * 相同图入库
	 */
	@RequestMapping("/insertForSame")
	@ResponseBody
	public Map insertForSame(HttpServletRequest request) throws IOException, ServiceException  { 
		Map<String,String> map=RequestUtil.getRequestAllParam(request);
		String tags = GetParamUtils.getParam("tags",map);
		//Brief
		int loginCompanyId = Integer.valueOf(GetParamUtils.getParam("loginCompanyId",map));
		String companyName = GetParamUtils.getParam("loginCompany",map);
		String orderNo = GetParamUtils.getParam("orderNo",map);
		String materialCode = GetParamUtils.getParam("materialCode",map);
		String remark = GetParamUtils.getParam("remark",map);
		Brief brief = new Brief(orderNo, materialCode,companyName,loginCompanyId,remark);
		//url 水印
		String url = GetParamUtils.getParam("url",map);
		url=QiniuExtraUrlParamUtils.waterMakerText(url, url, "仿宋", "580", "#5C2727", "1", null, null, null);
		return ResponseUtil.ResponseResult(BaiduSamePicSearchUtils.samePicInsertForRemote(brief, Tags.getSameTag(tags, loginCompanyId), url).toString());
	}
	
	/**
	 * 	相似图入库
	 * @param files
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	@RequestMapping("/insertForSimilar")
	@ResponseBody
	public Map insertForSimilar(HttpServletRequest request) throws IOException, ServiceException  {  
		Map<String,String> map=RequestUtil.getRequestAllParam(request);
		String tags = GetParamUtils.getParam("tags",map);
		//Brief
		int loginCompanyId = Integer.valueOf(GetParamUtils.getParam("loginCompanyId",map));
		String companyName = GetParamUtils.getParam("loginCompany",map);
		String orderNo = GetParamUtils.getParam("orderNo",map);
		String materialCode = GetParamUtils.getParam("materialCode",map);
		String remark = GetParamUtils.getParam("remark",map);
		Brief brief = new Brief(orderNo, materialCode,companyName,loginCompanyId,remark);
		//url 水印
		String url = GetParamUtils.getParam("url",map);
		url=QiniuExtraUrlParamUtils.waterMakerText(url, url, "仿宋", "580", "#5C2727", "1", null, null, null);
		return ResponseUtil.ResponseResult(BaiduSimilarPicSearchUtils.similarPicInsertForRemote(brief, Tags.getSameTag(tags, loginCompanyId), url).toString());
		
	}
	
	/**
	 * 	商品图入库
	 * @param files
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	@RequestMapping("/insertForProduct")
	@ResponseBody
	public Map insertForProduct(HttpServletRequest request) throws IOException, ServiceException  {  
		Map<String,String> map=RequestUtil.getRequestAllParam(request);
		//Brief
		int loginCompanyId = Integer.valueOf(GetParamUtils.getParam("loginCompanyId",map));
		String companyName = GetParamUtils.getParam("loginCompany",map);
		String orderNo = GetParamUtils.getParam("orderNo",map);
		String materialCode = GetParamUtils.getParam("materialCode",map);
		String remark = GetParamUtils.getParam("remark",map);
		Brief brief = new Brief(orderNo, materialCode,companyName,loginCompanyId,remark);
		//url 水印
		String url = GetParamUtils.getParam("url",map);
		url=QiniuExtraUrlParamUtils.waterMakerText(url, url, "仿宋", "580", "#5C2727", "1", null, null, null);
		
		int class_id1 = Integer.valueOf(GetParamUtils.getParam("class_id1",map));
		int class_id2 = Integer.valueOf(GetParamUtils.getParam("class_id2",map));
		
		return ResponseUtil.ResponseResult(BaiduProductPicSearchUtils.productPicInsertForRemote(brief,  url, class_id1, class_id2).toString());
	}
	//图片删除
	/**
	 * 相同图删除
	 */
	@RequestMapping("/deleteForSame")
	@ResponseBody
	public Map deleteForSame(HttpServletRequest request) throws IOException, ServiceException  {  
		Map<String,String> map=RequestUtil.getRequestAllParam(request);
		String url = GetParamUtils.getParam("url",map);
		url=QiniuExtraUrlParamUtils.waterMakerText(url, url, "仿宋", "580", "#5C2727", "1", null, null, null);
		return ResponseUtil.ResponseResult(BaiduSamePicSearchUtils.samePicDeleteForRemote(url).toString());
	}
	
	/**
	 * 	相似图删除
	 * @param files
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	@RequestMapping("/deleteForSimilar")
	@ResponseBody
	public Map deleteForSimilar(HttpServletRequest request) throws IOException, ServiceException  {  
		Map<String,String> map=RequestUtil.getRequestAllParam(request);
		String url = GetParamUtils.getParam("url",map);
		url=QiniuExtraUrlParamUtils.waterMakerText(url, url, "仿宋", "580", "#5C2727", "1", null, null, null);
		return ResponseUtil.ResponseResult(BaiduSimilarPicSearchUtils.similarPicDeleteForRemote(url).toString());
	}
	
	/**
	 * 	商品图入库
	 * @param files
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	@RequestMapping("/deleteForProduct")
	@ResponseBody
	public Map deleteForProduct(HttpServletRequest request) throws IOException, ServiceException  {  
		Map<String,String> map=RequestUtil.getRequestAllParam(request);
		String url = GetParamUtils.getParam("url",map);
		//url 加水印
		url=QiniuExtraUrlParamUtils.waterMakerText(url, url, "仿宋", "580", "#5C2727", "1", null, null, null);
		return ResponseUtil.ResponseResult(BaiduProductPicSearchUtils.productPicDeleteForRemote(url).toString());
	}
	
	//图片查询
	/**
	 * 相同图查询
	 * @param files
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	@RequestMapping("/selectForSame")
	@ResponseBody
	public Map uploadSelectForSame(@RequestParam("file") CommonsMultipartFile files[],HttpServletRequest request) throws IOException, ServiceException  {  
		Map<String,String> map=RequestUtil.getRequestAllParam(request);
		String tags = GetParamUtils.getParam("tags",map);
		int tagLogic= 0;//Integer.valueOf(GetParamUtils.getParam("tagLogic",request));
		int pn = Integer.valueOf(GetParamUtils.getParam("pn",map));
		int rn = Integer.valueOf(GetParamUtils.getParam("rn",map));
		int loginCompanyId = Integer.valueOf(GetParamUtils.getParam("loginCompanyId",map));
		rn = (pn-1)*rn+rn;
		pn = (pn-1)*rn;
		if (!files[0].isEmpty()) { 
			InputStream inputStream = files[0].getInputStream();
			byte[] image = ImageUtils.readInputStream(inputStream, "GET");
			return ResponseUtil.ResponseResult(BaiduSamePicSearchUtils.samePicSelectForLocal(Tags.getSameTag(tags,loginCompanyId), image, tagLogic, pn, rn).toString());
		}
		return ResponseUtil.ResponseResult(null);
	}
	
	/**
	 * 	相似图查询
	 * @param files
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	@RequestMapping("/selectForSimilar")
	@ResponseBody
	public Map uploadSelectForSimilar(@RequestParam("file") CommonsMultipartFile files[],HttpServletRequest request) throws IOException, ServiceException  {  
		Map<String,String> map=RequestUtil.getRequestAllParam(request);
		String tags = GetParamUtils.getParam("tags",map);
		int tagLogic= 0;Integer.valueOf(GetParamUtils.getParam("tagLogic",map));
		int pn = Integer.valueOf(GetParamUtils.getParam("pn",map));
		int rn = Integer.valueOf(GetParamUtils.getParam("rn",map));
		int loginCompanyId = Integer.valueOf(GetParamUtils.getParam("loginCompanyId",map));
		rn = (pn-1)*rn+rn;
		pn = (pn-1)*rn;
		if (!files[0].isEmpty()) { 
			InputStream inputStream = files[0].getInputStream();
			byte[] image = ImageUtils.readInputStream(inputStream, "GET");
			return ResponseUtil.ResponseResult(BaiduSimilarPicSearchUtils.similarPicSelectForLocal(Tags.getSameTag(tags,loginCompanyId), image, tagLogic, pn, rn).toString());
		}
		return ResponseUtil.ResponseResult(null);
	}
	
	/**
	 * 	商品图查询
	 * @param files
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws ServiceException
	 */
	@RequestMapping("/selectForProduct")
	@ResponseBody
	public Map uploadSelectForProduct(@RequestParam("file") CommonsMultipartFile files[],HttpServletRequest request) throws IOException, ServiceException  {  
		Map<String,String> map=RequestUtil.getRequestAllParam(request);
		int pn = Integer.valueOf(GetParamUtils.getParam("pn",map));
		int rn = Integer.valueOf(GetParamUtils.getParam("rn",map));
		int class_id1 = Integer.valueOf(GetParamUtils.getParam("class_id1",map));
		int class_id2 = Integer.valueOf(GetParamUtils.getParam("class_id2",map));
		rn = (pn-1)*rn+rn;
		pn = (pn-1)*rn;
		if (!files[0].isEmpty()) { 
			InputStream inputStream = files[0].getInputStream();
			byte[] image = ImageUtils.readInputStream(inputStream, "GET");
			return ResponseUtil.ResponseResult(BaiduProductPicSearchUtils.productPicSelectForLocal(image, pn, rn, class_id1, class_id2).toString());
		}
		return ResponseUtil.ResponseResult(null);
	}
	
}
